using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;
using Workflows.Components.DALC;

namespace Workflows.Components
{
    public partial class SubflowEditor : Form, IComponentEditor 
    {
        private Dictionary<int, WorkflowDesign> _AllWfDesigns;
        private object _OwnerActivityNode;
        private Subflow _Subflow;

        /// <summary></summary>
        public SubflowEditor()
        {
            InitializeComponent();
        }

        private void SubflowEditor_Load(object sender, EventArgs e)
        {
            this.cbo_Workflows.Items.Clear();
            this.cbo_Workflows.SelectedIndex = -1;
            this.cbo_Workflows.Text = string.Empty;
            this._AllWfDesigns=new Dictionary<int, WorkflowDesign>();
            List<WorkflowDesign> wfDesigns = WorkflowDesignDALC.GetAllWorkflowDesigns();
            if(wfDesigns !=null && wfDesigns.Count>0)
            {
                for (int i = 0; i < wfDesigns.Count;i++ )
                {
                    WorkflowDesign wfDesign = wfDesigns[i];
                    this.cbo_Workflows.Items.Add(wfDesign.WorkflowName);
                    this._AllWfDesigns.Add(i,wfDesign);
                }
            }
        }

        private void cbo_Workflows_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_Workflows.SelectedIndex>=0 && this._AllWfDesigns.ContainsKey(this.cbo_Workflows.SelectedIndex))
            {
                WorkflowDesign wfDesign = this._AllWfDesigns[this.cbo_Workflows.SelectedIndex];
                this.lbl_StepsForWorkflow.Text =
                    string.Format("Total steps for selected workflow: {0}", wfDesign.Components.Length);
            }
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(Subflow); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._Subflow = (Subflow) wfComponent;
            if(this._AllWfDesigns !=null && this._AllWfDesigns.Count>0)
            {
                foreach(int idx in this._AllWfDesigns.Keys)
                {
                    WorkflowDesign wfDesign = this._AllWfDesigns[idx];
                    if(wfDesign.WorkflowGuid==this._Subflow.SubFlowID)
                    {
                        if(this.cbo_Workflows.Items.Count >=idx)
                        {
                            this.cbo_Workflows.SelectedIndex = idx;
                        }
                        break;
                    }
                }
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            if (this.cbo_Workflows.SelectedIndex >= 0 && this._AllWfDesigns.ContainsKey(this.cbo_Workflows.SelectedIndex))
            {
                WorkflowDesign wfDesign = this._AllWfDesigns[this.cbo_Workflows.SelectedIndex];
                this._Subflow.SubFlowID = wfDesign.WorkflowGuid;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._Subflow);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public TestResult Test()
        {
            return new TestResult(true,string.Empty);
        }
    }
}